//
//  SearchViewController.m
//  ShunTian
//
//  Created by zhanshu on 15/8/28.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//



#import "SearchViewController.h"
#import "ProductCollectionView.h"
#import "ShowSearchVC.h"

@interface SearchViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,SearchProductDelegate>
{
    NSMutableArray *productsArr;
}
@property (weak, nonatomic) IBOutlet UIView *searchBackView;
@property (weak, nonatomic) IBOutlet UITextField *searchTextField;

@property (weak, nonatomic) IBOutlet UITableView *searchTableView;
@property (strong, nonatomic) NSArray *recordArr;
@property (strong, nonatomic) ProductCollectionView *prodictCV;


@property (weak, nonatomic) IBOutlet UIImageView *noProductImg;//没有商品时显示的图片

@end

@implementation SearchViewController
-(void)viewDidAppear:(BOOL)animated {
    [self.searchTextField becomeFirstResponder];
}
- (void)resetView {
    [self.view insertSubview:_noProductImg aboveSubview:_prodictCV];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navBar.hidden = YES;
    //刷新数据
    [self reloadData];
    productsArr = [[NSMutableArray alloc] init];
    _searchBackView.layer.cornerRadius = 15;
    _searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _searchTextField.delegate = self;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _prodictCV = [[ProductCollectionView alloc] initWithFrame:CGRectMake(0, 70, ScreenWidth, ScreenHeight-64) collectionViewLayout:layout];
    _prodictCV.hidden = YES;
    _prodictCV.searchProductDelegate = self;
    [self.view addSubview:_prodictCV];
    [self resetView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _recordArr.count+1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"搜索历史";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *Cell = @"cell";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:Cell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cell];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake((ScreenWidth)/2 - 60.5, 7, 121, 26);
        [btn setImage:[UIImage imageNamed:@"deletemamery"] forState:UIControlStateNormal];
        btn.tag = 100;
        btn.hidden = YES;
        [btn addTarget:self action:@selector(deleteRecord) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btn];
    }
    if (_recordArr.count>0) {
        UIButton *btn = (UIButton*)[cell.contentView viewWithTag:100];
        if (indexPath.row == _recordArr.count) {
            btn.hidden = NO;
            cell.textLabel.text = @"";
        }else {
            btn.hidden = YES;
            cell.textLabel.textAlignment = NSTextAlignmentLeft;
            cell.textLabel.text = [_recordArr objectAtIndex:indexPath.row];
        }
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.view endEditing:YES];
    if (indexPath.row == _recordArr.count) {
        //清空搜索记录
        [NSUserDefualManager storageValue:@[] forKey:SearchRecord];
        [self reloadData];
    }else {
        _searchTextField.text = _recordArr[indexPath.row];
        [self getMess];
    }
}
- (void)reloadData {
    _recordArr = [NSUserDefualManager getValueByKey:SearchRecord];
    if (_recordArr.count > 0) {
        _searchTableView.hidden = NO;
        [_searchTableView reloadData];
    }else {
        _searchTableView.hidden = YES;
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self.view endEditing:YES];
}
#pragma mark - 搜索获取数据
- (void)getMess {
    ShowSearchVC *order = [[ShowSearchVC alloc] init];
    order.keyWord = _searchTextField.text;
    [self.navigationController pushViewController:order animated:YES];
}
#pragma mark - 清除历史记录
- (void)deleteRecord {
    [NSUserDefualManager storageValue:@[] forKey:SearchRecord];
    [self reloadData];
}
#pragma mark - 搜索事件
- (IBAction)searchAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)searchManager {
    [self.view endEditing:YES];
    if (_searchTextField.text.length == 0) {
        [[MessShowView share] showWithMess:@"搜索内容不能为空"];
        return;
    }
    if (_searchTextField.text.length != 0) {
        //不能为空
        NSString *changedStr = [_searchTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        if (changedStr.length != 0) {
            [NSUserDefualManager addNewRecord:_searchTextField.text forKey:SearchRecord];
        }
    }
    [self reloadData];
    [self getMess];
}
#pragma mark - UITextField 
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    //搜索事件
    [self searchManager];
    return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField {
    _prodictCV.hidden = YES;
    _searchTableView.hidden = NO;
    _noProductImg.hidden = YES;
    return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
